package com.xm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.xm.entity.Banners;
import com.xm.uti.DBManager;
import com.xm.uti.QueryDataResult;

/**
 * 轮播图持久层的实现
 * 
 * @author kh
 *
 */
public class BannerDaoImpl {
	public ArrayList<Banners> getBanList() {
		String sql = "select * from banners where 1=1 order by weight desc";
		System.out.println("执行的sql语句是" + sql);
		QueryDataResult execQuery = DBManager.executeQuery(sql);
		ArrayList<Banners> list = new ArrayList<Banners>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					Banners e = new Banners(rs.getInt("id"), rs.getString("title"), rs.getString("url"),
							rs.getString("Link"), rs.getInt("weight"));
					list.add(e);
				}
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println(list);
		return list;
	}

	/**
	 * 增
	 * 
	 * @param emp
	 * @return
	 */
	public boolean insertBan(Banners banners) {
		String sql = "insert into banners (title,url,weight,link) values('" + banners.getTitle() + "','" + banners.getUrl()
				+ "','" + banners.getWeight() + "','" + banners.getLink() + "')";
		System.out.println("执行的sql语句是" + sql);
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 删除
	 * 
	 * @param emp
	 * @return
	 */
	public boolean delBanner(int id) {
		String sql = "delete from banners where id = " + id;
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 修改
	 * 
	 * @param emp
	 * @return
	 */
	public boolean updateBan(Banners banners) {
		System.out.println("轮播图的数据是"+banners);
		String sql = "update banners set title='" + banners.getTitle() + "',url='" + banners.getUrl() + "',link='"
				+ banners.getLink() + "',weight='" + banners.getWeight() + "' where id =" + banners.getId();
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

}